///////////////////////////////////////////////////////////////////////////////
// Project: Hoyo Client Game
//
// hy_player_db_logic.cpp
// Created: 2013-7-15
// Author: Like Cheng
///////////////////////////////////////////////////////////////////////////////

#include "hy_player_logic.h"
#include "hy_player_db_logic.h"

#include "hy_player_system/hy_player_data.h"

#include "hy_db/hy_db_logic.h"
#include "hy_db/hy_db_const.h"

#include "hy_net/hy_network_message.h"
#include "hy_net/hy_network_default.h"

#include "hy_common_system/hy_sequence_logic.h"

USING_NS_HY_NETWORK;
hy_player_db_logic* hy_player_db_logic_instance = NULL;
///////////////////////////////////////////////////////////////////////////////
//hy_player_db_logic::hy_player_db_logic
///////////////////////////////////////////////////////////////////////////////
hy_player_db_logic::hy_player_db_logic()
{
}
///////////////////////////////////////////////////////////////////////////////
//hy_player_db_logic::~hy_player_db_logic
///////////////////////////////////////////////////////////////////////////////
hy_player_db_logic::~hy_player_db_logic()
{

}
///////////////////////////////////////////////////////////////////////////////
//hy_player_db_logic::instance
///////////////////////////////////////////////////////////////////////////////
hy_player_db_logic* hy_player_db_logic::instance()
{
	if( hy_player_db_logic_instance == NULL )
		hy_player_db_logic_instance = new hy_player_db_logic;
	return hy_player_db_logic_instance;
}
///////////////////////////////////////////////////////////////////////////////
//hy_player_db_logic::purge_instance
///////////////////////////////////////////////////////////////////////////////
void hy_player_db_logic::purge_instance()
{
	DEL_POINTER( hy_player_db_logic_instance );
}
///////////////////////////////////////////////////////////////////////////////
//hy_player_db_logic::load_player_data_by_db
///////////////////////////////////////////////////////////////////////////////
void hy_player_db_logic::load_player_data_by_db( int player_id, bool is_update )
{
	std::string table = HY_DB_AND( HY_DB, HY_TABLE_PLAYER );

	mongo::BSONObjBuilder con;
	con.append( "player_id", player_id );

	hy_player_data* player_data = hy_player_logic::instance()->get_player_data_by_player_id( player_id );
	if( player_data )
	{
		if( is_update )
		{			
			Json::Value bvalue = hy_db_logic::instance(DP)->find_json_val( table.c_str(), con );			
			player_data->set_player_value( bvalue );
		}	
	}
	else
	{

		Json::Value bvalue = hy_db_logic::instance()->find_json_val( table.c_str(), con );
		hy_player_data* pd = new hy_player_data;
		pd->set_player_id( player_id );
		pd->set_player_value( bvalue );

		hy_player_logic::instance()->add_player_data( pd, is_update );
	}
}


bool hy_player_db_logic::save_player_data_by_db( int player_id )
{
	std::string table = HY_DB_AND( HY_DB, HY_TABLE_PLAYER );

	hy_player_data* player_data = hy_player_logic::instance()->get_player_data_by_player_id( player_id );
	if( player_data )
	{
		Json::Value& player_value = player_data->get_player_value();
		mongo::BSONObjBuilder con;
		con.append( "player_id", player_id );
		string k = player_value.toStyledString();
		int len = k.length();
		mongo::BSONObj b = mongo::fromjson(k.c_str(),&len);
		hy_db_logic::instance(DP)->update_bson( table.c_str(), con, b );

		return true;
	}
	else
		return false;

	return true;
}